package com.dog.modules.upms.producer;

import com.alibaba.fastjson2.JSON;
import com.dog.framework.core.constant.IotMqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @description 通知本体消息的生产者
 * @since 2022/4/15 14:22
 */
@Component
@Slf4j
public class NotifyRobotProducer<T> implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     */
    public void sendMsg(T entity) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(IotMqConstants.CLEANING_DIRECT_EXCHANGE, IotMqConstants.CLEANING_IOT_IN_ROUTING_KEY, JSON.toJSONString(entity), correlationId);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info(" 回调id:" + correlationData);
        if (ack) {
            log.info("消息成功消费");
        } else {
            log.info("消息消费失败:" + cause);
        }
    }
}
